﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:util="urn:utilities"            
    xmlns:v="urn:schemas-microsoft-com:vml"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    exclude-result-prefixes="msxsl v"
    extension-element-prefixes="util"
>
  
  <xsl:output method="xml" indent="no"/>

  <xsl:param name="DropShadowNoise" select="0.0"/>
  <xsl:param name="DropShadowSoftness" select="0.1"/>

  <xsl:variable name="PtToPixels" select="96 div 72"/>
  <xsl:variable name="TwipsToPixels" select="96 div 72 div 20"/>
  
  <xsl:template match="v:group">
    <xsl:comment>VML group</xsl:comment>
      <Canvas Width="{$TwipsToPixels * number(substring-before(@coordsize, ','))}" Height="{$TwipsToPixels * number(substring-after(@coordsize, ','))}">
        <Canvas.RenderTransform>
          <TranslateTransform X="{-$TwipsToPixels * number(substring-before(@coordorigin, ','))}" Y="{-$TwipsToPixels * number(substring-after(@coordorigin, ','))}"/>
        </Canvas.RenderTransform>
        <Canvas.Resources>
          <xsl:apply-templates select="v:shapetype" mode="resource"/>
        </Canvas.Resources>
        <xsl:apply-templates select="*"/>
      </Canvas>
  </xsl:template>

  <xsl:variable name="VMLStyles">
    <Style x:Key="TextBoxFlowDocumentPageViewerStyle" TargetType="{{x:Type FlowDocumentPageViewer}}">
      <Setter Property="Background" Value="#00FFFFFF"/>
      <Setter Property="HorizontalContentAlignment" Value="Center"/>
      <Setter Property="VerticalContentAlignment" Value="Center"/>
      <Setter Property="KeyboardNavigation.TabNavigation" Value="Local"/>
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{{x:Type FlowDocumentPageViewer}}">
            <DockPanel Language="en-us" Uid="DockPanel_2" LastChildFill="True">
              <Grid HorizontalAlignment="Stretch" Uid="Grid_23" Background="{{TemplateBinding Background}}" DockPanel.Dock="Bottom">
                <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="*"/>
                  <ColumnDefinition Width="Auto"/>
                  <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
              </Grid>
              <Border HorizontalAlignment="{{TemplateBinding HorizontalContentAlignment}}" Margin="{{TemplateBinding Padding}}" VerticalAlignment="{{TemplateBinding VerticalContentAlignment}}" Uid="Border_47" BorderBrush="{{TemplateBinding BorderBrush}}" BorderThickness="{{TemplateBinding BorderThickness}}">
                <AdornerDecorator ClipToBounds="True" Uid="AdornerDecorator_1">
                  <DocumentPageView Uid="DocumentPageView_1" KeyboardNavigation.IsTabStop="False" KeyboardNavigation.TabIndex="1" KeyboardNavigation.TabNavigation="Local" DocumentViewerBase.IsMasterPage="True" DocumentPaginator="{{x:Null}}" PageNumber="0"/>
                </AdornerDecorator>
              </Border>
            </DockPanel>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>

  </xsl:variable>

  <xsl:template match="v:shapetype">
    <xsl:comment>Parametric Shape Type</xsl:comment>
  </xsl:template>

  <xsl:template match="v:shapetype" mode="resource">
    <Path x:Key="{@id}">
      <xsl:if test="@path">
        <xsl:attribute name="Data">
          <xsl:value-of select="util:GetPathData(@path)"/>
        </xsl:attribute>
      </xsl:if>
    </Path>
  </xsl:template>

  <xsl:template match="v:shadow"/>

  <xsl:template name="LengthAttribute">
    <xsl:param name="attribute"/>
    <xsl:param name="value"/>
    
    <xsl:choose>
      <xsl:when test="contains($value,'pt')">
        <xsl:attribute name="{$attribute}">
          <xsl:value-of select="$value"/>
        </xsl:attribute>
      </xsl:when>
      <xsl:when test="number($value) != 'NaN'">
        <xsl:attribute name="{$attribute}">
          <xsl:value-of select="$value * $TwipsToPixels"/>
        </xsl:attribute>
      </xsl:when>
    </xsl:choose>

  </xsl:template>

  <xsl:template match="v:roundrect | v:rect">
    <Border x:Name="{@id}" BorderBrush="Black" Background="White" BorderThickness="1.5">
      <xsl:if test="@strokecolor">
        <xsl:attribute name="BorderBrush">
          <xsl:value-of select="concat('#FF', substring-before(substring-after(@strokecolor,'#'), ' '))"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:if test="@fillcolor">
        <xsl:attribute name="Background">
          <xsl:value-of select="concat('#FF', substring-before(substring-after(@fillcolor,'#'), ' '))"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:if test="@strokeweight">
        <xsl:attribute name="BorderThickness">
          <xsl:value-of select="$PtToPixels * number(substring-before(@strokeweight,'pt'))"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:variable name="left" select="substring-before(substring-after(concat(@style,';'),'left:'),';')"/>
      <xsl:variable name="top" select="substring-before(substring-after(concat(@style,';'),'top:'),';')"/>
      <xsl:variable name="height" select="substring-before(substring-after(concat(@style,';'),'height:'),';')"/>
      <xsl:variable name="width" select="substring-before(substring-after(concat(@style, ';'),'width:'),';')"/>
      <xsl:if test="@arcsize">
        <xsl:attribute name="CornerRadius">
          <xsl:value-of select="util:CornerRadius(@arcsize, $width, $height)"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:call-template name="LengthAttribute">
        <xsl:with-param name="value" select="$left"/>
        <xsl:with-param name="attribute" select="'Canvas.Left'"/>
      </xsl:call-template>
      <xsl:call-template name="LengthAttribute">
        <xsl:with-param name="value" select="$top"/>
        <xsl:with-param name="attribute" select="'Canvas.Top'"/>
      </xsl:call-template>
      <xsl:call-template name="LengthAttribute">
        <xsl:with-param name="value" select="$height"/>
        <xsl:with-param name="attribute" select="'Height'"/>
      </xsl:call-template>
        <xsl:call-template name="LengthAttribute">
          <xsl:with-param name="value" select="$width"/>
          <xsl:with-param name="attribute" select="'Width'"/>
        </xsl:call-template>
      <xsl:apply-templates select="v:shadow" mode="Shadow"/>
      <Canvas HorizontalAlignment="Left" VerticalAlignment="Top">
        <xsl:apply-templates select="*">
          <xsl:with-param name="width" select="$width"/>
          <xsl:with-param name="height" select="$height"/>
        </xsl:apply-templates>
      </Canvas>
    </Border>

  </xsl:template>

  <xsl:template match="v:shadow" mode="Shadow">
    <Border.Effect>
      <DropShadowEffect Color="Gray" BlurRadius="5">
        <xsl:if test="@color">
          <xsl:attribute name="Color">
            <xsl:value-of select="substring-before(@color, ' ')"/>
          </xsl:attribute>
        </xsl:if>
        <xsl:if test="@opacity">
          <xsl:attribute name="Opacity">
            <xsl:value-of select="@opacity"/>
          </xsl:attribute>
        </xsl:if>
        <xsl:if test="@offset">
          <xsl:attribute name="ShadowDepth">
            <xsl:value-of select="util:GetLength(@offset)"/>
          </xsl:attribute>
          <xsl:attribute name="Direction">
            <xsl:value-of select="util:GetAngle(@offset)"/>
          </xsl:attribute>
        </xsl:if>
      </DropShadowEffect> 
    </Border.Effect>
  </xsl:template>

  <xsl:template match="v:textbox">
    <xsl:param name="width"/>
    <xsl:param name="height"/>
    <xsl:variable name="vertical" select="substring-before(substring-after(concat(@style,';'),'layout-flow:'),';')='vertical'"/>
    
    <FlowDocumentPageViewer Style="{{StaticResource TextBoxFlowDocumentPageViewerStyle}}" RenderTransformOrigin="0,0" HorizontalAlignment="Left" VerticalAlignment="Top">
      <xsl:choose>
        <xsl:when test="$vertical">
          <xsl:call-template name="LengthAttribute">
            <xsl:with-param name="value" select="$height"/>
            <xsl:with-param name="attribute" select="'Width'"/>
          </xsl:call-template>
          <xsl:attribute name="Height">Auto</xsl:attribute>
          <FlowDocumentPageViewer.RenderTransform>
            <TransformGroup>
              <ScaleTransform ScaleX="1" ScaleY="1"/>
              <SkewTransform AngleX="0" AngleY="0"/>
              <RotateTransform Angle="-90"/>
              <TranslateTransform X="0">
                <xsl:call-template name="LengthAttribute">
                  <xsl:with-param name="value" select="$height"/>
                  <xsl:with-param name="attribute" select="'Y'"/>
                </xsl:call-template>
              </TranslateTransform>
            </TransformGroup>
          </FlowDocumentPageViewer.RenderTransform>
        </xsl:when>
        <xsl:otherwise>
          <xsl:call-template name="LengthAttribute">
            <xsl:with-param name="value" select="$width"/>
            <xsl:with-param name="attribute" select="'Width'"/>
          </xsl:call-template>
          <FlowDocumentPageViewer.RenderTransform>
            <TransformGroup>
              <ScaleTransform ScaleX="1" ScaleY="1"/>
              <SkewTransform AngleX="0" AngleY="0"/>
              <RotateTransform Angle="0"/>
              <TranslateTransform X="0" Y="0"/>
            </TransformGroup>
          </FlowDocumentPageViewer.RenderTransform>
        </xsl:otherwise>
      </xsl:choose>
      <xsl:apply-templates select="*"/>
    </FlowDocumentPageViewer>
  </xsl:template>

  <xsl:template match="v:shape">
    <xsl:comment>Parametric shape</xsl:comment>
    <!--
    <Path Stroke="Black" StrokeThickness="1" Data="{util:GetPathData(@data)}"/>
    -->
  </xsl:template>

  <xsl:template match="v:stroke">
    <xsl:comment>Stroke</xsl:comment>
  </xsl:template>

</xsl:stylesheet>
